VIEWS:

   Views are the virtual tables created from the existing physical tables.

Purpose of views:
1. It is used to apply the restriction to access the data.[Data abstraction (hides complexity)]
2. Joins can be converted into views and used.
3. Simplifies queries:
Instead of writing long queries repeatedly, you encapsulate them in a view and reuse it.


Note: 
1. Making changes in the main table will reflect in the views.
2. Making changes in the views also reflects in the main table.
3. Usually read-only views are used.



1. How to create views?
Ans:
(a) create view <viewName> as select * from <tableName>;
  Ex: create view v_sample as select * from sample;

(b) create view <viewName> as select <col1>,<col2>,... from <tableName>;
 Ex: create view v_sample as select id, name, city from sample;



2. How to insert the data into views?
Ans:
  (a) insert into <viewName> values(<val1>,<val2>,.....);

  (b) insert into <viewName> (<col1>,<col2>,...) values (<val1>,<val2>,....);

  (c) insert into <viewName> values (<&var1>,<&var2>,...);


3. How to update the records in views?
Ans:
   (a) update <viewName> set <colName>=<values>;

   (b) update <viewName> set <colName>=<values> where <condition>;



4. How to delete the view records?
Ans:
   (a) delete from <viewName>;

   (b) delete from <viewName> where <condition>;



5. how to drop the views?
Ans: dropping views will not drop the main table.
   drop view <viewName>;




6. How to convert joins output into views?
Ans:
create view <viewName> as select alias1.col1, alias1.col2, alias2.col1, alias2.col2 from <table1> alias1, <table2> alias2 where <condition>;
